Main components of an R-package


A cartoon of an R package

Why R is so useful



Easy to use


Easy to get help


A lot of work has been done already

Why R is so useful



Easy to use


Easy to get help


A lot of work has been done already

What is an R package (for the user)


A set of functions or data!

## Loading required package: sp
## class       : RasterLayer 
## dimensions  : 180, 360, 64800  (nrow, ncol, ncell)
## resolution  : 1, 1  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

Famous packages

Famous packages in ecology


More than XXX packges on CRAN!


vegan

lme, INLA

raster, sp

ape, phytools

The purpose of R packages


Distribute code

Distribute data

Organize analyses

Collaborate on a project

Where packages live



1. The Comprehensive R Archive Network


2. GitHub

1. The Comprehensive R Archive Network (CRAN)

1. The Comprehensive R Archive Network (CRAN)

We have all done it



2. GitHub

2. GitHub

Also easy



Developing an R package

An example from from the Bromeliaceae

An example from from the Bromeliaceae



An example from from the Bromeliaceae

## # A tibble: 3,272 x 12
##    tax_accepted_na~ tax_genus tax_tribus tax_subfamily geo_range
##    <chr>            <chr>     <chr>      <chr>             <dbl>
##  1 Acanthostachys ~ Acanthos~ <NA>       Bromelioideae      7695
##  2 Acanthostachys ~ Acanthos~ <NA>       Bromelioideae    613125
##  3 Aechmea abbrevi~ Aechmea   <NA>       Bromelioideae     15625
##  4 Aechmea aculeat~ Aechmea   <NA>       Bromelioideae     11499
##  5 Aechmea aenigma~ Aechmea   <NA>       Bromelioideae      7436
##  6 Aechmea aguadoc~ Aechmea   <NA>       Bromelioideae      7701
##  7 Aechmea aiuruoc~ Aechmea   <NA>       Bromelioideae      7705
##  8 Aechmea alba Mez Aechmea   <NA>       Bromelioideae     89375
##  9 Aechmea alegren~ Aechmea   <NA>       Bromelioideae      7703
## 10 Aechmea alopecu~ Aechmea   <NA>       Bromelioideae     27649
## # ... with 3,262 more rows, and 7 more variables: conv_conr_eoo <dbl>,
## #   conv_conr_aoo <dbl>, conv_conr_pop <dbl>, conv_conr_code <chr>,
## #   conv_conr_stat <chr>, tr_growth_form <chr>, tax_canonical <chr>

An example from from the Bromeliaceae

## Simple feature collection with 3272 features and 1 field
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: -112.8833 ymin: -44.00848 xmax: -34.63333 ymax: 36.38029
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## First 10 features:
##                                               tax_accepted_name
## 1          Acanthostachys pitcairnioides (Mez) Rauh & Barthlott
## 2      Acanthostachys strobilacea (Schult. & Schult.f.) Klotsch
## 3                                    Aechmea abbreviata L.B.Sm.
## 4                Aechmea aculeatosepala (Rauh & Barthlott) Leme
## 5  Aechmea aenigmatica Lopez-Ferrari, Espejo, Ceja et A.Mendoza
## 6                        Aechmea aguadocensis Leme & L.Kollmann
## 7                                     Aechmea aiuruocensis Leme
## 8                                              Aechmea alba Mez
## 9                                    Aechmea alegrensis W.Weber
## 10                                       Aechmea alopecurus Mez
##                          geometry
## 1  POLYGON ((-41.478 -12.1831,...
## 2  MULTIPOLYGON (((-54.38333 -...
## 3  MULTIPOLYGON (((-75.88333 -...
## 4  POLYGON ((-78.1828 -3.26, -...
## 5  POLYGON ((-96.4525 16.1917,...
## 6  POLYGON ((-40.4689 -18.5706...
## 7  POLYGON ((-44.1391 -22.0335...
## 8  MULTIPOLYGON (((-40.13333 -...
## 9  POLYGON ((-41 -20.7089, -41...
## 10 POLYGON ((-43.6783 -22.5497...

An example from from the Bromeliaceae

An example from from the Bromeliaceae

What is an R package (for a developer)



A folder…

…with standardized structure and names

Package structure


DESCRIPTION

README.md

NAMSPACE

R/

man/

data/

inst/, vignettes/, tests/ .Rbuildignore, .gitignore, cran-comments.md, NEWS.md

Package structure

DESCRIPTION

A text file giving basic and standradized information on the package

DESCRIPTION

A screenshot of a description file, go through it and explain what to add

README.md

A free describtion of the package for github

Version number

What does the package do

How to contribute

Citation

README.md

A Picture of a markdown readme

README.md

A picture of a readme on github

NAMESPACE

A file with the names of all functions that your package uses and provides.

NAMESPACE

A picture of an old namespace file

data/


The datasets distributed with the package. Can be example data or the purpose of the package.

data/


Can be any object that can be loaded into R.

Tables, shape files

data/

R/

The code (functions) provided via your package.

Functions



In packages code is distributed as functions.
Functions are objects just as variables.

Functions

## [1] 4

For example

## [1] 4
## [1] 1001

For example

## [1] ":-)"
## [1] ":-("

R/

R/

man/



The documentation of your code. One file per function.

man/

## starting httpd help server ... done

man/

man/

man/

The rest


inst/ - non-text files (e.g. pdfs)

vignettes/ - Tutorials and documentation

test - automated tests of the code

.gitignore - files git should ignore

.Rbuildignore - files R CMD build should ignore

Build and check

Summary


An R package is a set of standardized folders

Description - basic information

Readme - basic information

data/ - datasets of the package

R/ - the functions of the package

man/ - the documentation of the code

others - to facilitate the developement progress and provide additional information

How to develope an R-package



Rstudio, Github, Git, devtools, roxygen2


Rstudio workflow. Next lecture!

Your project!



Chose a dataset from your research that you want to provide as an R package, and one or two basic functions to use it.